//
//  BaseTableView.m
//  YYBG_IOS
//
//  Created by wyl on 2017/5/12.
//  Copyright © 2017年 wyl. All rights reserved.
//

#import "BaseTableView.h"
#import "BSCommon.h"
@interface BaseTableView (){
    BaseNoDate *_noContentView;
    UIView *loadingView  ;
}
@end

@implementation BaseTableView

/**
 展示无数据占位图
 
 @param emptyViewType 无数据占位图的类型
 */
- (void)showEmptyViewWithType:(NoContentType)type title:(NSString *)title {
    
    // 如果已经展示无数据占位图，先移除
    if (_noContentView) {
        [_noContentView removeFromSuperview];
        _noContentView = nil;
    }
    //------- 再创建 -------//
    _noContentView = [[BaseNoDate alloc]initWithFrame:self.bounds];
    [self addSubview:_noContentView];
    [_noContentView setTypeAndTitle:type title:title];
    
    //------- 添加单击手势 -------//
    [_noContentView addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(noContentViewDidTaped:)]];
}
/* 移除无数据占位图 */
- (void)removeEmptyView{
    [_noContentView removeFromSuperview];
    _noContentView = nil;
    if (self.noContentViewTapedBlock){
        self.noContentViewTapedBlock = nil;//防止重复调用
    }
}

// 无数据占位图点击
- (void)noContentViewDidTaped:(BaseNoDate *)noContentView{
    if (self.noContentViewTapedBlock){
        self.noContentViewTapedBlock();//调用回调函数
        [_noContentView removeFromSuperview];
        _noContentView = nil;
    }
}



- (void)showLoading{
    if (loadingView) {
        [loadingView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
        [loadingView removeFromSuperview];
        loadingView = nil ;
    }
    loadingView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
    loadingView.centerX = self.width /2 ;
    loadingView.centerY = self.height /2 ;
    [loadingView setBorder:1 cornerRadius:10];
    loadingView.backgroundColor = [UIColor whiteColor];
    [self addSubview:loadingView];
    [loadingView bringSubviewToFront:self];
    
    UIImageView * loadingImg = [[UIImageView alloc]initWithImage:ImageNamed(@"load_01")];
    loadingImg.height = 40 ;
    loadingImg.width = 40 ;
    loadingImg.y = 10 ;
    loadingImg.centerX = 50 ;
    [loadingView addSubview:loadingImg];
    
 
    //创建一个可变数组
    NSMutableArray *ary=[NSMutableArray new];
    for(int I=1;I<=6;I++){
        //通过for 循环,把我所有的 图片存到数组里面
        NSString *imageName=[NSString stringWithFormat:@"load_0%d",I];
        UIImage *image=[UIImage imageNamed:imageName];
        [ary addObject:image];
    }
    // 设置图片的序列帧 图片数组
    loadingImg.animationImages=ary;
    //动画重复次数
    loadingImg.animationRepeatCount=MAXFLOAT;
    //动画执行时间,多长时间执行完动画
    loadingImg.animationDuration=0.3;
    //开始动画
    [loadingImg startAnimating];
    
    
    
    UILabel *label = [BSWidget commonLableWithFrame:CGRectMake(0, loadingImg.maxY+13, 60, 25) text:@"正在加载" color:[UIColor colorWithHexString:COLOR_DARK_GRAY] font:14];
    label.textAlignment = NSTextAlignmentCenter;
    label.centerX = 50 ;
    [loadingView addSubview:label];
    
}
-(void)hideLoading{
    if (loadingView) {
        [loadingView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
        [loadingView removeFromSuperview];
        loadingView = nil ;
    }
}



@end
